//
//  KBaseRequest.m
//  Peanut
//
//  Created by Jim huang on 2018/3/12.
//  Copyright © 2018年 Jim_Peanut. All rights reserved.
//

#import "KBaseRequest.h"
#import "KBaseAPI.h"

@interface KBaseRequest ()

@property (nonatomic,assign) YTKRequestMethod RequestMethodType;

@property (nonatomic,copy) NSString * requestUrlStr;

@end

@implementation KBaseRequest


- (instancetype)initPOSTWithRequestUrl:(NSString *)requestUrl{
    return [self initWithMethod:YTKRequestMethodPOST requestUrl:requestUrl];
}
- (instancetype)initGETWithRequestUrl:(NSString *)requestUrl{
    return [self initWithMethod:YTKRequestMethodGET requestUrl:requestUrl];
}

-(instancetype)initWithMethod:(YTKRequestMethod)requestMethod requestUrl:(NSString *)requestUrl{
    if (self = [super init]) {
        self.RequestMethodType = requestMethod;
        self.requestUrlStr = requestUrl;
        self.paramsDic = [NSMutableDictionary dictionary];
    }
    return self;
}


////添加请求头
//-(NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary{
//    
//    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
//    
//    if (![[KuserInfo shareInstance].userModel.sid isNotBlank]) {
//        [JLProgressHUDManager showErrorHud:@"sid缺失"];
//        return nil;
//    }
//    [dic setValue:[KuserInfo shareInstance].userModel.sid forKey:@"@sid"];
//    
//    return dic;
//}


// 设置超时时间
-(NSTimeInterval)requestTimeoutInterval{

    return 10;
}

- (NSString *)baseUrl{
    if ([self.requestBaseUrl isNotBlank]) {
        return self.requestBaseUrl;
    }
    return KBaseURL;
}


- (YTKRequestMethod)requestMethod{
    return self.RequestMethodType;
}

-(NSString *)requestUrl{


    return self.requestUrlStr;
}

-(id)requestArgument
{
    return self.paramsDic;
}

/**
 开始请求
 
 @param successBlock 成功回调
 @param failureBlock 失败回调
 */
- (void)newStartRequestWithCompletion:(void(^)(KBaseModel *baseModel))successBlock
                              failure:(void(^)(KBaseModel *errorModel))failureBlock{
    
   
    
    [self startWithCompletionBlockWithSuccess:^(__kindof YTKRequest * _Nonnull request)
     {
         KBaseModel * baseModel = [KBaseModel modelWithJSON:request.responseJSONObject];
         baseModel.request = request;
         if ([baseModel.statusCode isEqualToString:@"200"]) {
             !successBlock?:successBlock(baseModel);
         }else{
             !failureBlock?:failureBlock(baseModel);
         }
     }failure:^(__kindof YTKRequest * _Nonnull request)
     {

         KBaseModel * baseModel = [KBaseModel modelWithJSON:request.responseString];

         baseModel.request = request;
         
         
         
         if ([baseModel.statusCode isEqualToString:@"84512"]) {
//             [[KuserInfo shareInstance] logout];
             [JLProgressHUDManager showErrorHud:@"登录超时"];
             
             baseModel.msg = @"登录超时";
             [[KuserInfo shareInstance]logout];
         }else{
            baseModel.msg = @"请求发生错误,请稍后再试";

         }
         

         
         !failureBlock?:failureBlock(baseModel);
         


         
     }];
}

@end
